package alternate;


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * The CSVReader Class reads the content of a csv file.
 * @author Galina Bulgakova
 * @author Peter Weirich
 */
public class CSVReader 
{
    private String directory;
    
    /**
     * Constructor of CSV Reader. Requires the path to the csv file.
     * @param tempDirectory path + filename of the csv file.
     */
    public CSVReader(String tempDirectory)
    {
        this.directory = tempDirectory;
    }
    
    /**
     *Change the directory where the .csv file is located
     * @param tempDirectory Directory of the csv file
     */
    public void setDirectory(String tempDirectory)
    {
        this.directory = tempDirectory;
    }
    
    /**
     * Return the content of the .csv file as Map<String,ArrayList>
     * @param String: Name of the attribute
     * @param ArrayList: Content of the attribute
     * @return 
     */
    public Map<String,ArrayList> getCSVasMap()
    {
        Map mpResult = new HashMap<String,ArrayList>();
        
        try 
        {
            BufferedReader br = new BufferedReader( new FileReader(this.directory)); // read the file
            
            ArrayList csvContent = new ArrayList(); // Store the content of the lines of the csv in an ArrayList (2 dimensional)

            while((this.directory = br.readLine()) != null) 
            {
                String[] result = this.directory.split(",");    // parse the lines of the csv and add the created Array to the ArrayList
                csvContent.add(result);
            }
            
            for(int i=0; i< ((String[])csvContent.get(0)).length; i++) // Iterate through attributes
            {
                String tempAttributeName = null;                // temporary storage for the name of the attribute (column)
                ArrayList tempAttributeList = new ArrayList();  // temporary storage for the content of the attribute (lines)
                
                for(int j = 0; j < csvContent.size(); j++) // Iterate through the length of the list
                {
                    if(j == 0)
                    {
                        tempAttributeName = ((String[])csvContent.get(j))[i];   // get the Name of the attribute
                    }
                    else
                    {
                        tempAttributeList.add(((String[])csvContent.get(j))[i]);// get the content of the attribute
                    }
                }
                mpResult.put(tempAttributeName, tempAttributeList); // add the attribute and its content to the map
            }
        }

        catch (FileNotFoundException e) 
        {
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
        
        return mpResult;
    }
    
    /**
     * Returns the created Map as String
     * @return String of from getCSVasArray created Map
     */
    public String toString()
    {        
        return getCSVasMap().toString();
    }
}
